/*
 * @Date: 2024-03-10 21:42:10
 * @LastEditors: 玄绮 shijiahansi@qq.com
 * @LastEditTime: 2024-04-13 13:35:20
 * @FilePath: \server\src\server\app\ChatRoom.h
 */
#ifndef CHATROOM_H
#define CHATROOM_H
#pragma once

#include "ChatSession.h"

#include <ChatParticipant.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/signals2.hpp>
#include <boost/signals2/connection.hpp>
#include <data/Message.pb.h>
#include <data/UserInfo.pb.h>
#include <mysql/MysqlUtils.h>
#include <set>

class ChatRoom
    : public ChatParticipant,
      public MysqlUtils,
      public boost::enable_shared_from_this<ChatRoom> {
public:
    using SharedPtr              = boost::shared_ptr<ChatRoom>;
    using signalRoomNewMessage_t = boost::signals2::signal<void(data::Message msg)>;
    using MessageVector          = std::vector<data::Message>;

protected:
    data::ChatRoomInfo m_info;
    MessageVector m_messages;
    // std::set<std::string> m_users;
    std::vector<data::UserInfo> m_users;

public:
    ChatRoom(data::ChatRoomInfo info);
    ~ChatRoom();

public:
    signalRoomNewMessage_t signalRoomNewMessage;

public:
    virtual void deliver(data::Message msg);
    const MessageVector& getMessages() const;
    const std::vector<data::UserInfo>& getMembers() const;
    data::ChatRoomInfo getInfomation() const;
    void setInfomation(data::ChatRoomInfo);
    void join(std::string userId);
    void join(std::string userId, ChatSession::SharedPtr session);
    void leave(std::string userId);
    bool contains(std::string userId);
    std::string roomid();

    const data::ChatRoomInfo &info();

    void initConnect();

private:
    void slotOnUserLogin(std::string userId, ChatSession::SharedPtr session);
    void slotOnUserUnLogin(std::string userId, ChatSession::SharedPtr session);
};

#endif